import 'package:catguard/pages/home/home_logic.dart';
import 'package:catguard/routers/routers.dart';
import 'package:catguard/views/common_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:line_awesome_flutter/line_awesome_flutter.dart';
import '../../../config/config.dart';
import '../../../generated/assets.dart';
import '../../../global/global_data.dart';

class FragmentRecord extends StatelessWidget {
  FragmentRecord({super.key});

  final logic = Get.find<HomeLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg,
      resizeToAvoidBottomInset: false,
      body: SafeArea(
        child: Stack(
          children: [
            //添加宠物的提示
            Obx(
              () => Visibility(
                  visible: global.pets.isEmpty,
                  child: Column(
                    children: [
                      Container(
                        width: Get.width,
                        height: 380.w,
                        margin: EdgeInsets.symmetric(horizontal: 35.w, vertical: 80.w),
                        decoration: BoxDecoration(
                          color: Config.colorBg2,
                          borderRadius: BorderRadius.circular(Config.roundBig.w),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Opacity(
                              opacity: 0.4,
                              child:
                                  Image.asset(Assets.imagesPetNormal, width: 200.w, height: 200.w),
                            ),
                            SizedBox(
                              height: 30.w,
                            ),
                            Text("快来记录和猫咪的点点滴滴吧~",
                                style: TextStyle(
                                    color: Config.colorText3, fontSize: Config.textSizeContent.sp)),
                          ],
                        ),
                      ),
                      SizedBox(height: 70.w),
                      CommonWidget.commonButton("添加宠物", width: 600.w, radius: 48.w, () {
                        Get.toNamed(Routes.addPet);
                      }),
                    ],
                  )),
            ),
            Obx(
              () => Visibility(
                visible: global.pets.isNotEmpty,
                child: Column(
                  children: [
                    //顶部栏
                    Container(
                      width: Get.width,
                      padding: EdgeInsets.symmetric(horizontal: 35.w),
                      height: 128.w,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          GestureDetector(
                            onTap: () {
                              popPetManager();
                            },
                            child: Row(
                              children: [
                                Text(global.pets[global.currentPet.value]['name'],
                                    style: TextStyle(
                                        color: Config.colorText1,
                                        fontSize: 40.sp,
                                        fontWeight: FontWeight.bold)),
                                Icon(
                                  Icons.arrow_drop_down_rounded,
                                  size: 54.w,
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    headView(),
                    Obx(
                      () => logic.currentPetRecords.isNotEmpty
                          ? ListView.builder(
                              itemCount: 1,
                              shrinkWrap: true,
                              padding: const EdgeInsets.symmetric(vertical: 0),
                              itemBuilder: (context, index) {
                                return Container();
                              })
                          : Column(
                              children: [
                                Container(
                                  width: Get.width,
                                  height: 380.w,
                                  margin: EdgeInsets.symmetric(horizontal: 35.w, vertical: 30.w),
                                  decoration: BoxDecoration(
                                    color: Config.colorBg2,
                                    borderRadius: BorderRadius.circular(Config.roundBig.w),
                                  ),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Opacity(
                                        opacity: 0.4,
                                        child: Image.asset(Assets.imagesPetNormal,
                                            width: 200.w, height: 200.w),
                                      ),
                                      SizedBox(
                                        height: 30.w,
                                      ),
                                      Text("未记录，快来记录和猫咪的点点滴滴吧~",
                                          style: TextStyle(
                                              color: Config.colorText3,
                                              fontSize: Config.textSizeContent.sp)),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                    ),
                  ],
                ),
              ),
            ),
            Positioned(
              right: 50.w,
              bottom: 50.w,
              child: InkWell(
                onTap: (){
                  popAddRecord();
                },
                child: Container(
                  width: 110.w,
                  height: 110.w,
                  decoration: BoxDecoration(
                      color: Config.colorMain.withOpacity(0.5),
                      borderRadius: BorderRadius.circular(55.w),
                      border: Border.all(color: Config.colorText1, width: 5.w)),
                  child: Icon(LineAwesomeIcons.pencil_alt_solid,size: 60.w,color: Config.colorText1,),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  headView() {
    return Container(
      width: Get.width,
      margin: EdgeInsets.symmetric(horizontal: 35.w),
      padding: EdgeInsets.symmetric(vertical: 35.w, horizontal: 35.w),
      decoration: BoxDecoration(
        color: Config.colorBg2,
        borderRadius: BorderRadius.circular(Config.roundBig.w),
      ),
      child: Column(
        children: [
          Row(
            children: [
              //头像
              Container(
                width: 110.w,
                height: 110.w,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(55.w),
                  color: Config.colorMain.withOpacity(0.2),
                ),
                clipBehavior: Clip.antiAlias,
                child: Image.asset(Assets.imagesPetNormal),
              ),
              SizedBox(width: 22.w),
              //名称和信息
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text("二五八万",
                          style: TextStyle(
                              color: Config.colorText1,
                              fontSize: Config.textSizeTitle.w,
                              fontWeight: FontWeight.w500)),
                      Text("（布偶猫）",
                          style: TextStyle(
                              color: Config.colorText3,
                              fontSize: Config.textSizeInfo.w,
                              height: 1.7,
                              fontWeight: FontWeight.w500)),
                    ],
                  ),
                  SizedBox(
                    height: 10.w,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Icon(
                        LineAwesomeIcons.mars_solid,
                        size: 28.w,
                        color: Colors.redAccent.withOpacity(0.8),
                      ),
                      SizedBox(
                        width: 4.w,
                      ),
                      RichText(
                        text: TextSpan(
                          text: ' ', // 文本内容
                          style: TextStyle(
                              fontSize: Config.textSizeInfo.w, color: Config.colorText3), // 基础样式
                          children: [
                            TextSpan(
                              text: '1年3个月20天', // 子文本
                              style: TextStyle(
                                fontSize: Config.textSizeInfo.w,
                                color: Config.colorText3,
                              ),
                            ),
                            TextSpan(
                              text: '    2.5KG',
                              style: TextStyle(
                                fontSize: Config.textSizeInfo.w,
                                color: Config.colorText3,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  )
                ],
              )
            ],
          ),
          SizedBox(
            height: 25.w,
          ),
          //与你相伴
          Container(
            height: 60.w,
            width: double.infinity,
            padding: EdgeInsets.symmetric(horizontal: 20.w),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15.w),
              color: Config.colorMain.withOpacity(0.3),
            ),
            clipBehavior: Clip.antiAlias,
            alignment: Alignment.centerLeft,
            child: Text("已与你相伴100天~",
                style: TextStyle(
                    color: Config.colorText1,
                    fontSize: Config.textSizeInfo.w,
                    fontWeight: FontWeight.w500)),
          ),
        ],
      ),
    );
  }

  menuItem(int index, Function() onTap) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 35.w),
        height: 114.w,
        child: Row(
          children: [
            Text(
              global.pets[index]['name'],
              style: TextStyle(color: Config.colorText1, fontSize: Config.textSizeSubTitle.sp),
            ),
            Expanded(
              child: Obx(
                    () => Container(
                  alignment: Alignment.centerRight,
                  child: Visibility(
                    visible: index == global.currentPet.value,
                    child: Icon(
                      Icons.check_rounded,
                      size: 46.w,
                      color: Config.colorText1,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  popPetManager() {
    Widget widget = Column(
      children: [
        Container(
          width: Get.width,
          margin: EdgeInsets.only(left: 35.w, right: 35.w, top: 110.w),
          decoration: BoxDecoration(
            color: Config.colorBg,
            borderRadius: BorderRadius.circular(Config.roundBig.w),
          ),
          clipBehavior: Clip.antiAlias,
          child: Column(
            children: [
              Obx(
                    () => ListView.builder(
                    itemCount: global.pets.length,
                    shrinkWrap: true,
                    itemBuilder: (context, index) {
                      return Column(
                        children: [
                          menuItem(index, () {
                            global.currentPet.value = index;
                            Get.back();
                          }),
                          Visibility(
                            visible:
                            global.pets.length > 1 && index < global.pets.length - 1,
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 35.w),
                              child: CommonWidget.commonDivider(),
                            ),
                          ),
                        ],
                      );
                    }),
              ),
              InkWell(
                onTap: () {
                  Get.back();
                  Future.delayed(const Duration(milliseconds: 200),(){
                    Get.toNamed(Routes.petManager);
                  });
                },
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 35.w),
                  height: 114.w,
                  color: Config.colorBg2,
                  child: Row(
                    children: [
                      Text(
                        "宠物管理",
                        style: TextStyle(
                            color: Config.colorText1, fontSize: Config.textSizeSubTitle.sp),
                      ),
                      Expanded(
                          child: Container(
                            alignment: Alignment.centerRight,
                            child: Icon(
                              LineAwesomeIcons.paw_solid,
                              size: 46.w,
                              color: Config.colorText1,
                            ),
                          ))
                    ],
                  ),
                ),
              )
            ],
          ),
        )
      ],
    );
    Get.dialog(widget);
  }

  popAddRecord(){
    Widget widget= Container(
      height: 570.w,
      width: Get.width,
      decoration: const BoxDecoration(
        color: Config.colorBg2,
        borderRadius: BorderRadius.only(topLeft: Radius.circular(Config.roundBig),
        topRight: Radius.circular(Config.roundBig)),
      ),
      child: Column(
        children: [
          Container(
            alignment: Alignment.center,
            height: 130.w,
            child:  Text("创建记录",
                style: TextStyle(
                    color: Config.colorText1,
                    fontSize: Config.textSizeSubTitle.sp)),
          ),
          Wrap(
            spacing: 35.w,
            runSpacing: 35.w,
            children: [
              wrapItem("体重",LineAwesomeIcons.weight_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addWeight);
                });
              }),
              wrapItem("吃喝",LineAwesomeIcons.fish_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addEat);
                });
              }),
              wrapItem("病历",LineAwesomeIcons.notes_medical_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addCase);
                });
              }),
              wrapItem("疫苗",LineAwesomeIcons.syringe_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addVaccine);
                });
              }),
              wrapItem("用药",LineAwesomeIcons.capsules_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addDrug);
                });
              }),
              wrapItem("洗澡",LineAwesomeIcons.shower_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addShower);
                });
              }),
              wrapItem("驱虫",LineAwesomeIcons.prescription_bottle_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addDisinsection);
                });
              }),
              wrapItem("记笔记",LineAwesomeIcons.pen_alt_solid,(){
                Get.back();
                Future.delayed(const Duration(milliseconds: 300),(){
                  Get.toNamed(Routes.addNote);
                });
              }),
            ],
          ),
        ],
      ),
    );
    Get.bottomSheet(widget);
  }

  wrapItem(String name,IconData icon,Function() onTap){
    return GestureDetector(
      onTap: onTap,
      child: Column(
        children: [
          Container(
            width: 130.w,
            height: 130.w,
            decoration: BoxDecoration(color: Config.colorMain.withOpacity(0.3),
                borderRadius: BorderRadius.circular(65.w)),
            child: Icon(
              icon,size: 66.w,
              color: Config.colorText1,
            ),
          ),
          SizedBox(height: 5.w,),
          Container(
            width: 130.w,
            height: 40.w,
            alignment: Alignment.center,
            child:  Text(name,
                style: TextStyle(
                    color: Config.colorText1,
                    fontSize: Config.textSizeContent.sp)),
          )
        ],
      ),
    );
  }
}
